package com.woniuxy.oa.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.woniuxy.oa.entity.AddressMsg;
import com.woniuxy.oa.entity.EmployeeAddress;
import com.woniuxy.oa.entity.EmployeeMsg;
import com.woniuxy.oa.entity.PageMsg;
import com.woniuxy.oa.service.AddressService;
import com.woniuxy.oa.utils.RegexUtil;

@Controller
public class AddressController {

	@Autowired
	@Qualifier("AddressService")
	AddressService addressService;
	
	//展示地址
	@RequestMapping("/displayAddress")
	public String selectAllAddress(Model model,int pageIndex,int pageSize,HttpSession session) {
		EmployeeMsg employeeMsg =new EmployeeMsg();
		if(session.getAttribute("condition")!=null) {
			 employeeMsg=(EmployeeMsg) session.getAttribute("condition");
		}
		PageMsg pageMsg = addressService.selectEmployeeMsgByCondition(employeeMsg, pageIndex, pageSize);
		model.addAttribute("pageMsg", pageMsg);
		//System.out.println(pageMsg);
		//因为属性为对象的时候，该对象的属性没办法取出来，因此新建一个实体类来存储每个信息。
		List<EmployeeMsg> employeeMsgs = pageMsg.getEmployeeMsgs();
		List<EmployeeAddress> employeeAddresses=new ArrayList<EmployeeAddress>();
		//System.out.println("666"+employeeMsgs);
		for(int i=0;i<employeeMsgs.size();++i) {
			if(employeeMsgs.get(i).getAddressMsg()!=null) {
				EmployeeAddress employeeAddress=new EmployeeAddress(employeeMsgs.get(i).getId(),
					employeeMsgs.get(i).getUsername(),employeeMsgs.get(i).getAddressMsg().getA_address(),
					employeeMsgs.get(i).getAddressMsg().getA_tellphone(),employeeMsgs.get(i).getAddressMsg().getA_fixedphone(),
					employeeMsgs.get(i).getAddressMsg().getA_emergency());
			employeeAddresses.add(employeeAddress);
			}else {
				EmployeeAddress employeeAddress=new EmployeeAddress();
				employeeAddress.setId(employeeMsgs.get(i).getId());
				employeeAddress.setUsername(employeeMsgs.get(i).getUsername());
			}
		}
		//System.out.println(employeeAddresses);
		model.addAttribute("employeeAddress", employeeAddresses);
		return "system/addressList";
	}
	
	//展示地址
		@RequestMapping("/displayAddressByCondition")
		public String selectAddressByCondition(EmployeeMsg employeeMsg,Model model,int pageIndex,int pageSize,HttpSession session) {
			//将查询条件放入session中，在上一页和下一页等查询的时候可用
			session.setAttribute("condition", employeeMsg);
			PageMsg pageMsg = addressService.selectEmployeeMsgByCondition(employeeMsg, pageIndex, pageSize);
			model.addAttribute("pageMsg", pageMsg);
			//System.out.println(pageMsg);
			//因为属性为对象的时候，该对象的属性没办法取出来，因此新建一个实体类来存储每个信息。
			List<EmployeeMsg> employeeMsgs = pageMsg.getEmployeeMsgs();
			List<EmployeeAddress> employeeAddresses=new ArrayList<EmployeeAddress>();
			//System.out.println("666"+employeeMsgs);
			for(int i=0;i<employeeMsgs.size();++i) {
				if(employeeMsgs.get(i).getAddressMsg()!=null) {
					EmployeeAddress employeeAddress=new EmployeeAddress(employeeMsgs.get(i).getId(),
						employeeMsgs.get(i).getUsername(),employeeMsgs.get(i).getAddressMsg().getA_address(),
						employeeMsgs.get(i).getAddressMsg().getA_tellphone(),employeeMsgs.get(i).getAddressMsg().getA_fixedphone(),
						employeeMsgs.get(i).getAddressMsg().getA_emergency());
				employeeAddresses.add(employeeAddress);
				}else {
					EmployeeAddress employeeAddress=new EmployeeAddress();
					employeeAddress.setId(employeeMsgs.get(i).getId());
					employeeAddress.setUsername(employeeMsgs.get(i).getUsername());
				}
			}
			//System.out.println(employeeAddresses);
			model.addAttribute("employeeAddress", employeeAddresses);
			return "system/addressList";
		}
	
	
	
	
	
	//修改地址
	@RequestMapping("/changeAddress")
	public String changAddress(AddressMsg addressMsg,Model model) {
		System.out.println(addressMsg);
		//验证输入格式是否正确
		//判断用户名
		boolean flag=true;
		if(!RegexUtil.isUsername(addressMsg.getUsername())) {
			model.addAttribute("userName", "用户名格式不正确，必须是5-20位字母");
			flag=false;
		};
		if(!RegexUtil.isMobile(addressMsg.getA_tellphone())) {
			model.addAttribute("mobile", "手机号格式不正确");
			flag=false;
		}
		if(!RegexUtil.isEmail(addressMsg.getA_email())) {
			model.addAttribute("email", "邮箱格式不正确");
			flag=false;
		}
		if(!flag) {
			return "modify";
		}
		addressService.modifyAddressMsg(addressMsg);
		return "/system/index/index";
	}
	
	
	
	
	
	
	
	
	//删除员工地址信息
	@RequestMapping("/deleteEmployee")
	public String deleteAddress(int id) {
		addressService.deleteAddressMsg(id);
		return "/system/index/index";
	}
	
	
	
	
	
	
	
	
	
	
}
